package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.bean.OrderDetail;
import com.itheima.bean.Orders;
import com.itheima.bean.ShoppingCart;
import com.itheima.common.BaseContext;
import com.itheima.common.R;
import com.itheima.dto.OrdersDto;
import com.itheima.service.OrderDetailService;
import com.itheima.service.OrdersService;
import com.itheima.service.ShoppingCartService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author Tang
 * @date 2023/2/25
 */
@RequestMapping("/order")
@RestController
public class OrdersController {
    @Autowired
    private OrdersService ordersService;

    @Autowired
    private OrderDetailService orderDetailService;

    @Autowired
    private ShoppingCartService shoppingCartService;

    /**
     * 提交订单
     * @param orders
     * @return
     */
    @PostMapping("/submit")
    public R<String> submit(@RequestBody Orders orders) {
        //1.调用service的提交订单方法
        ordersService.submit(orders);
        //2.返回结果
        return R.success("提交订单成功！");
    }

    /**
     * 订单明细分页查询
     * @param page
     * @param pageSize
     * @param beginTime
     * @param endTime
     * @return
     */
    @GetMapping("/page")
    public R<Page<Orders>> page(@RequestParam(defaultValue = "1") Integer page,
                                @RequestParam(defaultValue = "10")Integer pageSize,
                                String number,
                                @DateTimeFormat(pattern = "yyyy-MM-dd") String beginTime,
                                @DateTimeFormat(pattern = "yyyy-MM-dd")String endTime){

        Page<Orders> ordersPage = new Page<>(page, pageSize);

        LambdaQueryWrapper<Orders> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(number!=null,Orders::getNumber,number);
        if (beginTime!=null){
            wrapper.between(Orders::getOrderTime,beginTime,endTime);
        }

        wrapper.orderByDesc(Orders::getOrderTime);

        ordersService.page(ordersPage,wrapper);
        return R.success(ordersPage);
    }

    /**
     * 订单派送成功|完成
     * @param orders
     * @return
     */
    @PutMapping
    public R<String> order(@RequestBody Orders orders){
        LambdaQueryWrapper<Orders> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Orders::getId,orders.getId());

        boolean flag = ordersService.updateById(orders);
        return flag ? R.success("订单派送成功") : R.error("订单派送失败");

    }


    /**
     * 分页查询用户订单分页
     * @param page
     * @param pagesize
     * @return
     */
    @GetMapping("/userPage")
    public R<Page<OrdersDto>> userPage(@RequestParam(defaultValue = "1") int page, @RequestParam(defaultValue = "10")int pagesize){
        //调用service方法
        Page<OrdersDto> ordersDtoPage = ordersService.getUserPage(page,pagesize);
        //返回结果
        return R.success(ordersDtoPage);
    }

    @PostMapping("/again")
    public R<String> againSubmit(@RequestBody Map<String,String> map){
        String ids = map.get("id");
        long id = Long.parseLong(ids);

        LambdaQueryWrapper<OrderDetail> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(OrderDetail::getOrderId,id);
        List<OrderDetail> orderDetailList = orderDetailService.list(queryWrapper);

        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ShoppingCart::getId,id);
        shoppingCartService.remove(wrapper);

        Long currentId = BaseContext.getCurrentId();
        List<ShoppingCart> shoppingCartList = orderDetailList.stream().map((item)->{
            ShoppingCart shoppingCart = new ShoppingCart();
            BeanUtils.copyProperties(item,shoppingCart);
            shoppingCart.setUserId(currentId);
            Long dishId = item.getDishId();
            Long setmealId = item.getSetmealId();
            String dishFlavor = item.getDishFlavor();
            if(dishId != null){
                shoppingCart.setDishId(dishId);
                shoppingCart.setDishFlavor(dishFlavor);
            }else {
                shoppingCart.setSetmealId(setmealId);
            }
            shoppingCart.setName(item.getName());
            shoppingCart.setNumber(item.getNumber());
            shoppingCart.setAmount(item.getAmount());
            shoppingCart.setImage(item.getImage());
            shoppingCart.setCreateTime(LocalDateTime.now());
            return shoppingCart;
        }).collect(Collectors.toList());
        shoppingCartService.saveBatch(shoppingCartList);
        return R.success("再来一单成功");
    }
}

